package ui.forms;

import base.*;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Display;
import com.sun.lwuit.plaf.UIManager;
import com.sun.lwuit.util.Resources;
import db.*;
import java.util.Vector;
import utils.Config;
import utils.DateUtils;

public class Context extends javax.microedition.midlet.MIDlet {

    public static User currentUser;
    public static Resources mainResource;

    protected void startApp() {
        Display.init(this);

        //#mdebug

        try {

            if (System.getProperty("microedition.platform").equalsIgnoreCase("j2me")) {
                User meku = new User("7e623644-1161-4e91-a8d8-7925b4d5b039", "shayan", "1");
                meku.setDateOfBirth(DateUtils.stringToDate("01/01/1980"));
                meku.setHeight(160);
                meku.setWeight(60);
                db.UserManager.persist(meku);
            }

            Vector v = IntakeManager.getAll();
            for (int i = 0; i < v.size(); i++) {
                Intake intake = (Intake) v.elementAt(i);
                System.out.println(
                        "User:" + intake.getUser().getName()
                        + " Food: "+intake.getFoodId()+
                        " Q:"+intake.getQuantity()+
                        " Unit: "+intake.getUnit()+
                        " Date:"+intake.getDate().toString());

            }

        } catch (Exception ex) {
            ex.printStackTrace();
            Dialog.show("Error", ex.toString(), "OK", "");
        }

        //#enddebug

        //set the theme
        try {
            Context.mainResource = Resources.open("/ui/UItheme.res");
            UIManager.getInstance().setThemeProps(
                    Context.mainResource.getTheme(
                    Context.mainResource.getThemeResourceNames()[0]));

            //Dialog.show("Id", Config.getRememberedUserId(), "OK", "");
            String userId = Config.getRememberedUserId();
            if (userId != null && !userId.equals("")) {
                Context.currentUser = UserManager.getById(userId);
            }
            if (Context.currentUser != null) {
                MainMenu form = new MainMenu(this);
                form.show();
            } else {
                LoginForm form = new LoginForm(this);
                form.show();
            }

        } catch (Exception ex) {
            ex.printStackTrace();
            Dialog.show("Error", ex.toString(), "OK", "");
        }
    }

    protected void pauseApp() {
    }

    protected void destroyApp(boolean arg0) {
    }
}
